﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Globalization;

public partial class Programare : System.Web.UI.Page
{
    ServerObject mServerObject = null;
    public string tabelOreDisponibile = string.Empty;

    protected override void InitializeCulture()
    {
        System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(HttpContext.Current.Request.UserLanguages[0]);
        System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo(HttpContext.Current.Request.UserLanguages[0]);

        base.InitializeCulture();
    }

    private bool CheckServerObject()
    {
        bool result = false;

        if (Session["ServerModule"] != null)
        {
            mServerObject = (ServerObject)Session["ServerModule"];
        }
        else
        {
            mServerObject = new ServerObject();
            Session["ServerModule"] = mServerObject;
        }

        if (mServerObject != null)
        {
            result = true;
        }

        return result;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request["action"] != null)
        {
            string action = Request["action"].ToString();

            switch (action)
            {
                case "logout":
                    Session["UserData"] = null;
                    Session["ServerModule"] = null;
                    Response.Redirect("Default.aspx", true);
                    break;
                default:
                    break;
            }
        }

        DataTable userData = (DataTable)Session["UserData"];


        if (CheckServerObject())
        {
            if (!IsPostBack)
            {
                FillCounryCountryCombo();
                FillServcesDDL();
            }
        }
        else
        {
            //ShowPanel(stapeZeroPanel.ID);
            Response.Redirect("home.aspx", true);
        }
    }

    protected void FillCounryCountryCombo()
    {
        DataTable categoryList = mServerObject.GetCountryList();
        mServerObject.fillSelector(tariDDL, categoryList, "DenumireJudet", "IdJudet");
    }

    protected void FillServcesDDL()
    {
        DataTable serviceList = mServerObject.GetServicesCategoryList();
        mServerObject.fillSelector(servicesDDL, serviceList, "Denumirea", "ID Service");
    }

    protected void countryMissionDropDownList_SelectedIndexChanged(object sender, EventArgs e)
    {
        string stringIdTara = tariDDL.SelectedValue;
        int idTara = 0;
        int.TryParse(stringIdTara, out idTara);

        DataTable judeteList = mServerObject.GetJudeteList(idTara);
        if (judeteList != null && judeteList.Rows.Count > 0)
        {
            mServerObject.fillFrontPageSelector(judeteDDL, judeteList, "DenumireJudet", "IdJudet");
            nameConsulatLabel.Text = string.Empty;
            idConsulatHiddenField.Value = string.Empty;
        }
        else
        {
            judeteDDL.DataSource = null;

            DataTable consulatTable = mServerObject.GetConsulatListByCountryID(idTara);

            if (consulatTable != null && consulatTable.Rows.Count == 1)
            {
                nameConsulatLabel.Text = consulatTable.Rows[0]["Denumire"].ToString();
                idConsulatHiddenField.Value = consulatTable.Rows[0]["idConsulat"].ToString();
            }
        }
    }

    protected void judeteDDL_SelectedIndexChanged(object sender, EventArgs e)
    {
        int idJudet = 0;
        int.TryParse(judeteDDL.SelectedValue, out idJudet);

        DataTable consulatTable = mServerObject.GetConsulatListByCountryID(idJudet);

        if (consulatTable != null && consulatTable.Rows.Count == 1)
        {
            nameConsulatLabel.Text = consulatTable.Rows[0]["Denumire"].ToString();
            idConsulatHiddenField.Value = consulatTable.Rows[0]["idConsulat"].ToString();
        }
        else
        {
            nameConsulatLabel.Text = string.Empty;
            idConsulatHiddenField.Value = string.Empty;
        }

    }

    protected void RegForm3Calendar_DayRender(object sender, DayRenderEventArgs e)
    {
        if (e.Day.Date <= DateTime.Now.Date)
        {
            e.Day.IsSelectable = false;
        } 
    }

    private string ReturnDinamcTable(DataTable inputDT, int border)
    {
        string result = string.Empty;

        if (inputDT != null && inputDT.Rows.Count > 0)
        {
            result += "<Table cellpadding=\"5\" border=\"" + border + "\">";

            for (int i = 0; i < inputDT.Rows.Count; )
            {
                result += "<tr>";

                for (int j = 0; j < 4 && j < inputDT.Rows.Count; j++)
                {
                    result += "<td>";
                    //result += "<a href=\"?programare=" + stringConsulat + "_" + stringServiciu + "_" + stringData + "_" + stringHour + "\">" + stringHour + "</a>";
                    if (i < inputDT.Rows.Count)
                        result += inputDT.Rows[i][0].ToString();
                    result += "</td>";
                    i++;
                }

                result += "</tr>";
            }
            result += "</Table>";
        }
        else
        {
            result += "<p> NU SUNT ORE DISPONIBILE </p>";
        }

        return result;
    }

    protected void RegForm3Calendar_SelectionChanged(object sender, EventArgs e)
    {
        int idConsulat = 0;
        int idService = 0;
        DateTime selectedData = RegForm3Calendar.SelectedDate;

        int.TryParse(idConsulatHiddenField.Value.ToString(), out idConsulat);
        int.TryParse(servicesDDL.SelectedValue.ToString(), out idService);

        DataTable infoTable = mServerObject.GetAviableServiceInterval(idConsulat, idService, selectedData, 1);
        //DataTable infoTable = mServerObject.GetAviableServiceIntervalForOneDay(idConsulat, idService, selectedData);

        tabelOreDisponibile = ReturnDinamcTable(infoTable, 2);

    }
}